package com.gs.dao.jdbc.impl;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.gs.bean.New;
import com.gs.dao.NewDAO;
import com.gs.dao.jdbc.impl.rowmapper.NewRowMapper;
import com.liang.log.Log;
import com.liang.util.Page;

public class NewDAOImpl extends BaseDAOImpl implements NewDAO {

	@Override
	public void addNew(New newNew) {
		String sql = "insert into new_info(title,context,creationTime) values(?,?,?)";
		jdbcTemplate.update(sql, new Object[]{
				newNew.getTitle(),
				newNew.getContext(),
				newNew.getCreationTime()
		}, new int[]{Types.VARCHAR,Types.VARCHAR,Types.DATE});
	}

	@Override
	public void deleteNew(Integer id) {
		String sql = "delete new_info where id = ?";
		jdbcTemplate.update(sql,new Object[]{id},new int[]{Types.INTEGER});
	}

	@Override
	public New findNew(Integer id) {
		String sql = "select * from new_info where id = ?";
		New obj = null;
		obj =(New)jdbcTemplate.queryForObject(sql, new Object[]{id},new NewRowMapper());
		return obj;
	}

	@Override
	public List<New> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<New> queryPage(New searchModel, int currentPage, int pageSize) throws Exception{
		Page<New> page  = null;
		String _queryQuery = "select * from New_info";
		StringBuffer _queryWhere = new StringBuffer();
		List<New> modules = null;
		if(null == searchModel){
			modules = jdbcTemplate.query(_queryQuery, new NewRowMapper());
		}else{
			List<Object> _args = new ArrayList<Object>(0);
			List<Integer> _argTypeObjs = new ArrayList<Integer>(0);
			
			if(searchModel.getId() != null){
				_queryWhere.append(" id = ?");
				_args.add(searchModel.getId());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(searchModel.getTitle() != null){
				_queryWhere.append(" title like ?");
				_args.add('%'+searchModel.getTitle()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getContext() != null){
				_queryWhere.append(" context like ?");
				_args.add('%'+searchModel.getContext()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(searchModel.getStartCreationTime() != null){
				SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
				
				_queryWhere.append(" creationTime >= ?");
				_args.add(sdf.parse(searchModel.getStartCreationTime()));
				_argTypeObjs.add(Types.DATE);
			}
			if(searchModel.getEndCreationTime() != null){
				SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
				_queryWhere.append(" creationTime <= ?");
				_args.add(sdf.parse(searchModel.getEndCreationTime()));
				_argTypeObjs.add(Types.DATE);
			}
			
			if(!_queryWhere.toString().equals("")){
				_queryWhere.insert(0, " where");
			}
			_queryQuery += _queryWhere.toString();
			
			Log.info("sql:"+_queryQuery);
			int[] _argTypes = new int[_argTypeObjs.size()];
			for(int i = 0 ;  i < _argTypeObjs.size() ; i++){
				_argTypes[i] = _argTypeObjs.get(i);
			}
			modules = jdbcTemplate.query(_queryQuery, _args.toArray(), _argTypes, new NewRowMapper());
		}
		
		if(null == modules){
			modules = new ArrayList<New>(0);
		}
		
		page = new Page<New>(modules , currentPage , pageSize , modules.size());
		return page;
	}

	@Override
	public void updateNew(New updateNew) {
		// TODO Auto-generated method stub

	}

}
